package com.skin.admin.controller;

import com.skin.admin.entity.Users;
import com.skin.tools.RedisUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.auth.Subject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/23 0023.
 */
@Controller
public class IndexController {


    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/login")
    public String login(HttpServletRequest request,Model model){
        String msg = "";
            String exception = (String) request.getAttribute("shiroLoginFailure");
            if (exception != null) {
               msg= loginmessage(exception);
            }
        model.addAttribute("msg", msg);
        return "/login";
    }


    @RequestMapping({"/","/index"})
    public String index(HttpServletRequest request){
        Users users =(Users)SecurityUtils.getSubject().getPrincipal();
        request.getSession().setAttribute("sessionUsers",users);
        return "index/index";
    }

    @RequestMapping("/logout")
    public String logout(){
        SecurityUtils.getSubject().logout();
        return "login";
    }

    @RequestMapping("/403")
    public String doGetAuthorizationInfo(){
        return "/common/403";
    }


    private String loginmessage(String exception){
        String msg;
        if (UnknownAccountException.class.getName().equals(exception)) {
            System.out.println("UnknownAccountException -- > 账号不存在：");
            msg = " -- > 账号不存在：";
        } else if (IncorrectCredentialsException.class.getName().equals(exception)) {
            System.out.println("IncorrectCredentialsException -- > 密码不正确：");
            msg = " -- > 密码不正确：";
        } else if ("kaptchaValidateFailed".equals(exception)) {
            System.out.println(" kaptchaValidateFailed-- > 验证码错误");
            msg = " -- > 验证码错误";
        } else if(AuthenticationException.class.getName().equals(exception)){
            System.out.println("AuthenticationException -- > 密码存在问题");
            msg = " -- > 密码存在问题";
        }else {
            msg = "else >> " + exception;
            System.out.println("else -- >" + exception);
        }
        return msg;
    }

    @RequestMapping(value = "/first", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> firstResp (HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        request.getSession().setAttribute("message", request.getRequestURL());
        map.put("request Url", request.getRequestURL());
        return map;
    }

    @RequestMapping(value = "/sessions", method = RequestMethod.GET)
    @ResponseBody
    public Object sessions (HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        map.put("sessionId", request.getSession().getId());
        map.put("message", request.getSession().getAttribute("message"));
        return map;
    }
}
